package nc.ui.med.pub.query;

import java.util.ArrayList;

import nc.ui.pub.beans.UIComboBox;
import nc.ui.pub.beans.UIRefPane;
import nc.ui.pub.beans.constenum.IConstEnum;
import nc.ui.querytemplate.QueryConditionEditorContext;
import nc.ui.querytemplate.meta.FilterMeta;
import nc.ui.querytemplate.value.RefValueObject;
import nc.ui.querytemplate.valueeditor.DefaultFieldValueElementEditorFactory;
import nc.ui.querytemplate.valueeditor.IFieldValueElementEditor;
import nc.ui.querytemplate.valueeditor.RefElementEditor;
import nc.vo.jcom.lang.StringUtil;

public class MedDefaultFieldValueElementEditorFactory extends
		DefaultFieldValueElementEditorFactory {

	private QueryConditionBaseDLG m_dlg;

	public MedDefaultFieldValueElementEditorFactory(
			QueryConditionEditorContext context, QueryConditionBaseDLG dlg) {
		super(context);
		m_dlg = dlg;
	}

	@Override
	public IFieldValueElementEditor createFieldValueElementEditor(
			FilterMeta meta) {
		IFieldValueElementEditor ret = null;
		MedQueryFieldAddInfo addinfo = m_dlg.getMedTempInfo()
				.getSCMQueryFieldAddInfo(meta.getFieldCode());
		if (addinfo != null) {
			IConstEnum[] datas = addinfo.getComboxData();
			if (datas != null && datas.length > 0) {
				ret = getFieldValueElementEditor(datas);
			}
			if (addinfo.getRefCreator() != null)
				ret = new RefElementEditor(addinfo.getRefCreator()
						.getUIRefPane(), meta.getReturnType());
		}
		if (ret == null && m_dlg.getDefhelp() != null) {
			ret = m_dlg.getDefhelp().getFieldValueElementEditor(
					meta.getFieldCode(), meta);
		}
		if (ret == null)
			ret = super.createFieldValueElementEditor(meta);

		if (ret != null && addinfo != null
				&& ret.getFieldValueElemEditorComponent() != null
				&& ret.getFieldValueElemEditorComponent() instanceof UIRefPane) {
			UIRefPane refpane = (UIRefPane) ret
					.getFieldValueElemEditorComponent();
			if (!setInitWhere(meta.getFieldCode(), refpane)) {
				if (addinfo.getRefFixWhere() != null)
					refpane.setWhereString(QueryDlgUtil.andTowWhere(refpane
							.getRefModel().getWherePart(), addinfo
							.getRefFixWhere()));
			}
		}
		return ret;
	}

	public boolean setInitWhere(String fieldcode, UIRefPane refpane) {
		if (fieldcode == null || refpane == null)
			return false;
		ArrayList<String> reffields = m_dlg.getMedTempInfo().getRefFields(
				fieldcode);
		if (reffields == null || reffields.size() <= 0)
			return false;
		MedQueryFieldAddInfo refinfo = null;
		RefValueObject refvalue = null;
		ArrayList<Object[]> listrefinfos = null;

		String whereclause = null;
		for (String refcode : reffields) {
			refinfo = m_dlg.getMedTempInfo().getSCMQueryFieldAddInfo(refcode);
			if (refinfo == null
					|| refinfo.getFieldDefaultValue() == null
					|| !(refinfo.getFieldDefaultValue() instanceof RefValueObject))
				continue;
			refvalue = (RefValueObject) refinfo.getFieldDefaultValue();
			if (refvalue.getPk() == null)
				continue;
			listrefinfos = refinfo.getListRefInfos();
			if (listrefinfos == null || listrefinfos.size() <= 0)
				continue;

			for (Object[] info : listrefinfos) {
				if (!fieldcode.equals(info[MedQueryFieldAddInfo.RI_FIELD_CODE]))
					continue;
				whereclause = (String) info[MedQueryFieldAddInfo.RI_WHERE_CLAUSE];
				if (!StringUtil.isEmpty(whereclause)) {
					whereclause += " = '" + refvalue.getPk() + "' ";
					refpane.setWhereString(whereclause);
					return true;
				}
			}
		}
		return false;
	}

	public IFieldValueElementEditor getFieldValueElementEditor(
			IConstEnum[] datas) {
		UIComboBox uicombox = new UIComboBox();
		for (IConstEnum ce : datas) {
			uicombox.addItem(ce);
		}
		return new MedDefaultFieldValueElementEditor(uicombox);
	}

}
